package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者:hfj
 * 功能:菜单管理 前端控制器
 * 日期: 2024/4/19 10:32
 */
@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单管理")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     *
     * @return 查询所有菜单
     */
    @ApiOperation("查询所有菜单")
    @GetMapping
    public Result list(){
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }

    /**
     *
     * @param permission 权限实体类
     * @return 添加菜单
     */
    @ApiOperation("添加菜单")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission){
        boolean is_success = permissionService.save(permission);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param permission 权限实体类
     * @return 修改菜单
     */
    @ApiOperation("修改菜单")
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission){
        boolean is_success = permissionService.updateById(permission);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param id 菜单id
     * @return 递归删除菜单
     */
    @ApiOperation("递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

}
